#include <iostream>
#include <vector>
#include <string>
#include <unistd.h>
#include <sys/wait.h>
#include <signal.h>
#include <sstream>

using namespace std;

struct Process {
    pid_t pid;
    string name;
    bool running;
};

vector<Process> procs;

// 启动新进程
void start_process(const string& name, const string& args) {
    pid_t pid = fork();
    if (pid == -1) {
        cerr << "启动进程失败！" << endl;
        return;
    }
    if (pid == 0) {
        // 解析参数
        vector<char*> argv;
        argv.push_back((char*)name.c_str());
        stringstream ss(args);
        string arg;
        while (ss >> arg) {
            argv.push_back((char*)arg.c_str());
        }
        argv.push_back(nullptr);
        execvp(argv[0], argv.data());
        cerr << "进程执行失败！" << endl;
        exit(1);
    }
    procs.push_back({pid, name, true});
    cout << "已启动进程 " << name << "，PID: " << pid << endl;
}

// 显示进程列表
void list_processes() {
    cout << "\n进程列表：" << endl;
    cout << "ID\tPID\t名称\t状态" << endl;
    for (size_t i = 0; i < procs.size(); ++i) {
        cout << i << "\t" << procs[i].pid << "\t" 
             << procs[i].name << "\t" 
             << (procs[i].running ? "运行中" : "已停止") << endl;
    }
}

int main() {
    cout << "进程资源监控器" << endl;
    cout << "1. 启动进程  2. 列出进程  3. 暂停进程  4. 恢复进程  5. 终止进程  6. 退出" << endl;

    int choice;
    while (true) {
        cout << "\n请选择操作：";
        cin >> choice;

        switch (choice) {
            case 1: {
                string name, args;
                cout << "输入进程名称：";
                cin >> name;
                cin.ignore();
                cout << "输入参数：";
                getline(cin, args);
                start_process(name, args);
                break;
            }
            case 2:
                list_processes();
                break;
            case 3: {
                int id;
                cout << "输入要暂停的进程ID：";
                cin >> id;
                if (id >= 0 && (size_t)id < procs.size() && procs[id].running) {
                    kill(procs[id].pid, SIGSTOP);
                    cout << "进程已暂停" << endl;
                } else {
                    cout << "无效ID" << endl;
                }
                break;
            }
            case 4: {
                int id;
                cout << "输入要恢复的进程ID：";
                cin >> id;
                if (id >= 0 && (size_t)id < procs.size() && procs[id].running) {
                    kill(procs[id].pid, SIGCONT);
                    cout << "进程已恢复" << endl;
                } else {
                    cout << "无效ID" << endl;
                }
                break;
            }
            case 5: {
                int id;
                cout << "输入要终止的进程ID：";
                cin >> id;
                if (id >= 0 && (size_t)id < procs.size() && procs[id].running) {
                    kill(procs[id].pid, SIGKILL);
                    waitpid(procs[id].pid, nullptr, 0);
                    procs[id].running = false;
                    cout << "进程已终止" << endl;
                } else {
                    cout << "无效ID" << endl;
                }
                break;
            }
            case 6:
                // 清理所有进程
                for (auto& p : procs) {
                    if (p.running) {
                        kill(p.pid, SIGKILL);
                        waitpid(p.pid, nullptr, 0);
                    }
                }
                cout << "退出程序" << endl;
                return 0;
            default:
                cout << "无效操作" << endl;
        }
    }

    return 0;
}
